Implement a C# function to check whether a given string is a palindrome. A palindrome is a word, phrase, number, or other sequence of characters that reads the same forward and backward.
I completed my post-graduation in 2013 in the engineering field. Engineering is the application of science and math to solve problems. Engineers figure out how things work and find practical uses for scientific discoveries. Scientists and inventors often get the credit for innovations that advance the human condition, but it is engineers who are instrumental in making those innovations available to the world. I love pet animals such as dogs, cats, etc.
A palindrome number is a number that remains the same when its characters are reversed. In other words, it reads the same thing forward and backward.
Steps to Check Palindrome Number
To determine if a number is a palindrome, you can invert its digits and check if the inverted number is the same as the original number. Here is a quick step-by-step process.
Take the first number.
Turn his digits back.
Compare the opposite number to the first number.
If they are equal, the number is a palindrome.
Example-
Here is a simple c# example to demonstrate whether a given number is palindrome or not,
using System;
class Program
{
static void Main()
{
// take input from user and check where valid integer value or not
Console.WriteLine("Enter an integer to check if it is a palindrome:");
bool isSuccess = int.TryParse(Console.ReadLine(), out int number);
if (isSuccess)
{
// if enter valid value the check number is palindrome
if (IsPalindrome(number))
{
Console.WriteLine("{0} is a palindrome.", number);
}
else
{
Console.WriteLine("{0} is not a palindrome.", number);
}
}
else
{
Console.WriteLine("Invalid input. Please enter a valid integer.");
}
}
// function that take input number as argument
static bool IsPalindrome(int number)
{
// Negative numbers are not considered palindromes
if (number < 0)
{
return false;
}
// Get the reversed number
int reversed = ReverseNumber(number);
// Compare the original number with the reversed number
return number == reversed;
}
// function that reverse the entered number
static int ReverseNumber(int number)
{
int reversed = 0;
while (number > 0)
{
int remainder = number % 10;
reversed = (reversed * 10) + remainder;
number /= 10;
}
return reversed;
}
}
In the above example-
Main Method- Asks the user to input an integer and verifies that the input is a valid integer using int.TryParse. If true, the IsPalindrome method checks whether the integer is a palindrome.
IsPalindromeMethod- Checks if the integer is negative (since negative numbers are not treated as palindromes) and then compares the integer to its opposite value
ReverseNumber Method- Take the digits of an integer multiple times (using modulus 10), add them to the opposite number (multiply 10 each iteration to shift the digits left), and then subtract the last digit of the original number (use integer line 10).
Output-
1. Enter an integer to check if it is a palindrome:
123abc
Invalid input. Please enter a valid integer.
----------------------------------------------------------------
2. Enter an integer to check if it is a palindrome:
1234
1234 is not a palindrome.
----------------------------------------------------------------
3. Enter an integer to check if it is a palindrome:
12321
12321 is a palindrome.
This method works directly with an integer value without converting it to a string, making it possible to check if the integer is a palindrome.
Ashutosh Kumar Verma
14-Jun-2024Palindrome Checker in C#
A palindrome number is a number that remains the same when its characters are reversed. In other words, it reads the same thing forward and backward.
Steps to Check Palindrome Number
To determine if a number is a palindrome, you can invert its digits and check if the inverted number is the same as the original number. Here is a quick step-by-step process.
Example-
Here is a simple c# example to demonstrate whether a given number is palindrome or not,
In the above example-
Main Method- Asks the user to input an integer and verifies that the input is a valid integer using int.TryParse. If true, the IsPalindrome method checks whether the integer is a palindrome.
IsPalindromeMethod- Checks if the integer is negative (since negative numbers are not treated as palindromes) and then compares the integer to its opposite value
ReverseNumber Method- Take the digits of an integer multiple times (using modulus 10), add them to the opposite number (multiply 10 each iteration to shift the digits left), and then subtract the last digit of the original number (use integer line 10).
Output-
This method works directly with an integer value without converting it to a string, making it possible to check if the integer is a palindrome.
Also, Read: Roman to Integer